﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;

#endregion

namespace ScrumTable.UI.View.Util
{

    /// <summary>
    /// ViewElementUtils
    /// </summary>
    public class ViewElementUtils
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods

        /// <summary>
        /// Clones the UIElement.
        /// Reason: an UIElement can only have one parent.
        /// </summary>
        /// <param name="orig">The original.</param>
        /// <returns></returns>
        public static UIElement CloneElement(UIElement orig)
        {
            if (orig == null)
                return (null);

            var s = XamlWriter.Save(orig);
            var stringReader = new StringReader(s);
            var xmlReader = XmlReader.Create(stringReader, new XmlReaderSettings());
            return (UIElement)XamlReader.Load(xmlReader);
        }

        /// <summary>
        /// Finds the visual child.
        /// </summary>
        /// <typeparam name="TChildItem">The type of the child item.</typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem:class 
        {
            // Search immediate children first (breadth-first)
            for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                var child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is TChildItem)
                    return child as TChildItem;

                var childOfChild = FindVisualChild<TChildItem>(child);

                if (childOfChild != null)
                    return childOfChild;
            }

            return default(TChildItem);
        }

        /// <summary>
        /// Finds the visual parent.
        /// </summary>
        /// <typeparam name="TParentItem">The type of the parent item.</typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static TParentItem FindVisualParent<TParentItem>(object obj) where TParentItem : class
        {
            var findSource = obj as FrameworkElement;
            TParentItem draggedElement = null;
            while (draggedElement == null && findSource != null)
            {
                if ((draggedElement = findSource as TParentItem) == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }
            return draggedElement;
        }
     

        /// <summary>
        /// Finds the visual parent.
        /// </summary>
        /// <param name="typeParentItem">The type of the parent item.</param>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static object FindVisualParent(Type typeParentItem ,object obj) 
        {
            var findSource = obj as FrameworkElement;
            object draggedElement = null;
            while (draggedElement == null && findSource != null)
            {
                //typeParentItem.IsAssignableFrom()
                if( findSource.GetType() == typeParentItem)
                {
                    draggedElement = findSource;
                }
                if (draggedElement == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }
            return draggedElement;
        }
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion


    }
}
